<?php

namespace addons\litestore\controller\api;

use app\common\controller\Api;
use addons\litestore\model\Wxlitestoregoods;
use addons\litestore\model\Litestoregoodsspec;

class Goods extends Api
{
    protected $noNeedLogin = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    public function detail()
    {
        $goods_id = $this->request->request('goods_id');
        // 商品详情
        $detail = Wxlitestoregoods::detail($goods_id);
        $imgs = [];
        foreach (explode(",", $detail['images']) as $index => $item) {
            $imgs[] = cdnurl($item, true);
        }
        $detail['imgs_url'] = $imgs;

        if (!$detail || $detail['goods_status'] !== '10') {
            $this->error('很抱歉，商品信息不存在或已下架');
        }
        // 规格信息
        $specData = $detail['spec_type'] === '20' ? $detail->getManySpecData($detail['spec_rel'], $detail['spec']) : null;

        // 这里对规格的img格式化
        if ($specData != null) {
            foreach ($specData['spec_list'] as $index => $item) {
                $specData['spec_list'][$index]["form"]['imgshow'] = $specData['spec_list'][$index]["form"]['spec_image'] === '' ? null : cdnurl($specData['spec_list'][$index]["form"]['spec_image'], true);
            }
        }
        
        // unset($detail['spec_type']);
        unset($detail['deduct_stock_type']);
        unset($detail['images']);
        // unset($detail['sales_initial']);
        // unset($detail['sales_actual']);
        unset($detail['goods_sort']);
        unset($detail['delivery_id']);
        unset($detail['goods_status']);
        unset($detail['is_delete']);
        unset($detail['createtime']);
        unset($detail['updatetime']);

        // foreach($detail['spec'] as $v){
            // unset($v['goods_weight']);
            // unset($v['spec_sku_id']);
            // unset($v['spec_image']);
            // unset($v['create_time']);
            // unset($v['update_time']);

        // }
        $this->success('', [
            'detail'  => $detail,
            'specData' => $specData
        ]);
    }

    // 分类商品
    public function category_list()
    {
        $categoryid = $this->request->request('id');
        $page = $this->request->request('page');
        $sortType = $this->request->request('types');
        $rename = $this->request->request('name');

        // 筛选条件
        $filter = [];
        // if ($categoryid == -1) {
        //     $filter['goods_name'] = ['like', '%' . trim($rename) . '%'];
        // } else {
        $filter['a.category_id'] = $categoryid;
        // }
        $filter['a.goods_status'] = '10';
        $filter['a.is_delete'] = '0';

        // 排序规则
        $sort = [];
        if ($sortType === 'normal') {
            $sort = ['a.goods_sort' => 'desc', 'a.goods_id' => 'desc'];
        } elseif ($sortType === 'sales') {
            $sort = ['goods_sales' => 'desc'];
        } elseif ($sortType === 'price') {
            $sort = ['b.goods_price' => 'asc'];
        }

        // 商品表名称
        $model_temp = new Wxlitestoregoods();
        $listdata = $model_temp->field([
            '*', '(a.sales_initial + a.sales_actual)' => 'goods_sales',
        ])->alias('a')->join('litestore_goods_spec b','a.goods_id = b.goods_id')->where($filter)->order($sort)
            ->paginate(8, false, ['page' => $page]);

        $blistdatarList = [];
        foreach ($listdata as $index => $item) {
            $blistdatarList[] = [
                'image' => cdnurl(explode(",", $item['images'])[0], true), 'goods_name' => $item['goods_name'],
                'id' => $item['goods_id'], 'goods_price' => $item['spec'][0]['goods_price'], 'line_price' => $item['spec'][0]['line_price'], 'goods_sales' => $item['goods_sales']
            ];
        }

        $this->success('', [
            'listdata'  => $blistdatarList,
        ]);
    }
}
